//
//  YYAuthenticationService.m
//  CLNetWorking
//
//  Created by 亮 on 16/9/20.
//  Copyright © 2016年 亮. All rights reserved.
//

#import "YYAuthenticationService.h"
#import <CLNetWorkApiManager/CTNetworkingConfigurationManager.h>
#import "CTAppContext.h"
#import <CommonCrypto/CommonDigest.h>
#import <CLNetWorkApiManager/NSString+CTNetworkingMethods.h>

#define TimeStamp [[NSDate date] timeIntervalSince1970]
#define SALT @"qwertyasdfgzxcvb12345"

@implementation YYAuthenticationService


- (BOOL)isOnline {
    
    return [CTNetworkingConfigurationManager sharedInstance].serviceIsOnline;
}

//apiBaseUrl

- (NSString *)offlineApiBaseUrl {
    
    return @"http://172.20.87.24:8080/youyueyizu_api";
}

- (NSString *)onlineApiBaseUrl {
    
    return @"https://api.umedcn.com:8443/youyueyizu_api";
}

//version

- (NSString *)onlineApiVersion {
    
    return @"";
}

- (NSString *)offlineApiVersion {
    
    return @"";
}


//privateKey
- (NSString *)onlinePrivateKey {
    
    return @"";
}

- (NSString *)offlinePrivateKey {
    
    return @"";
}

//publicKey

- (NSString *)onlinePublicKey {
    
    return @"";
}

- (NSString *)offlinePublicKey {
    
    return @"";
}

- (NSDictionary *)extraParmas {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    int rand = arc4random();
    NSString *timetamp = [NSString stringWithFormat:@"%lf%d",TimeStamp,rand];
    NSString *token = [CTAppContext sharedInstance].accessToken;
    NSString *sign = [NSString stringWithFormat:@"%@%@%@",timetamp,token,SALT];
    sign = [sign CT_md5];
    [dic setObject:timetamp?:@"error" forKey:@"timestamp"];
    [dic setObject:token?:@"error" forKey:@"token"];
    [dic setObject:sign?:@"error" forKey:@"sign"];
    return dic;
}
- (BOOL)shouldCallBackByFailedOnCallingAPI:(CTURLResponse *)response {
    
    BOOL result = YES;
    if ([response.content[@"code"] isEqualToString:@"500"]) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:kBSUserTokenInvalidNotification
                                                            object:nil
                                                          userInfo:@{kBSUserTokenNotificationUserInfoKeyRequestToContinue:[response.request mutableCopy]?:@""
                                                                     ,kBSUserTokenNotificationUserInfoKeyManagerToContinue:self}];
        result = NO;
    }
    
    return result;

}
#undef TimeStamp
#undef SALT
@end
